package com.luckcoder.relax.games.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.luckcoder.relax.games.domain.bo.RelaxGameLeaderboardBo;
import com.luckcoder.relax.games.domain.vo.RelaxGameLeaderboardVo;
import com.luckcoder.relax.games.service.IRelaxGameLeaderboardService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 游戏排行榜
 *
 * @author zhanghao
 * @date 2025-09-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/games/gameLeaderboard")
public class RelaxGameLeaderboardController extends BaseController {
    
    private final IRelaxGameLeaderboardService relaxGameLeaderboardService;
    
    /**
     * 查询游戏排行榜列表
     */
    @SaCheckPermission("games:gameLeaderboard:list")
    @GetMapping("/list")
    public TableDataInfo<RelaxGameLeaderboardVo> list(RelaxGameLeaderboardBo bo, PageQuery pageQuery) {
        return relaxGameLeaderboardService.queryPageList(bo, pageQuery);
    }
    
    /**
     * 导出游戏排行榜列表
     */
    @SaCheckPermission("games:gameLeaderboard:export")
    @Log(title = "游戏排行榜", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RelaxGameLeaderboardBo bo, HttpServletResponse response) {
        List<RelaxGameLeaderboardVo> list = relaxGameLeaderboardService.queryList(bo);
        ExcelUtil.exportExcel(list, "游戏排行榜", RelaxGameLeaderboardVo.class, response);
    }
    
    /**
     * 获取游戏排行榜详细信息
     *
     * @param rankId 主键
     */
    @SaCheckPermission("games:gameLeaderboard:query")
    @GetMapping("/{rankId}")
    public R<RelaxGameLeaderboardVo> getInfo(@NotNull(message = "主键不能为空")
                                             @PathVariable Long rankId) {
        return R.ok(relaxGameLeaderboardService.queryById(rankId));
    }
    
    /**
     * 新增游戏排行榜
     */
    @SaCheckPermission("games:gameLeaderboard:add")
    @Log(title = "游戏排行榜", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RelaxGameLeaderboardBo bo) {
        return toAjax(relaxGameLeaderboardService.insertByBo(bo));
    }
    
    /**
     * 修改游戏排行榜
     */
    @SaCheckPermission("games:gameLeaderboard:edit")
    @Log(title = "游戏排行榜", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RelaxGameLeaderboardBo bo) {
        return toAjax(relaxGameLeaderboardService.updateByBo(bo));
    }
    
    /**
     * 删除游戏排行榜
     *
     * @param rankIds 主键串
     */
    @SaCheckPermission("games:gameLeaderboard:remove")
    @Log(title = "游戏排行榜", businessType = BusinessType.DELETE)
    @DeleteMapping("/{rankIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] rankIds) {
        return toAjax(relaxGameLeaderboardService.deleteWithValidByIds(List.of(rankIds), true));
    }
}
